package com.fht.service.food.impl;


import com.fht.constant.ManagermentSystemConstant;
import com.fht.entity.FoodMaterialManager;
import com.fht.entity.FoodRecord;
import com.fht.entity.FoodSpecification;
import com.fht.mapper.food.FoodMaterialManagerMapper;
import com.fht.service.food.FoodMaterialManagerService;
import com.fht.service.inventory.InventoryParticularsService;
import com.fht.vo.LayUITableVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class FoodMaterialManagerServiceImpl implements FoodMaterialManagerService {


    @Resource
    public FoodMaterialManagerMapper foodMaterialManagerMapper;

    @Autowired
    public InventoryParticularsService inventoryParticularsService;

    @Override
    public LayUITableVO<List<FoodMaterialManager>> findPage(Map<String, Object> map) {

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        List<FoodMaterialManager> list = foodMaterialManagerMapper.findPage(map);

        PageInfo<FoodMaterialManager> pageInfo = new PageInfo<>(list);

        List<FoodMaterialManager> foodMaterialManagerList = pageInfo.getList();
        for (FoodMaterialManager foodMaterialManager : foodMaterialManagerList) {
            String conversionName = foodMaterialManager.getConversion()+foodMaterialManager.getRepertoryUnitName()+"/"+foodMaterialManager.getPurchaseUnitName();
            foodMaterialManager.setConversionName(conversionName);

            foodMaterialManager.setUnitPriceString(String.valueOf(foodMaterialManager.getUnitPrice()/ManagermentSystemConstant.PRICE_TRANSITION));

        }



        LayUITableVO vo = new LayUITableVO();
        vo.setCount(pageInfo.getTotal());
        vo.setData(foodMaterialManagerList);


        return vo;

    }

    @Override
    public FoodMaterialManager selectById(String id) {
        FoodMaterialManager foodMaterialManager = foodMaterialManagerMapper.selectByPrimaryKey(id);

        foodMaterialManager.setUnitPriceString(String.valueOf(foodMaterialManager.getUnitPrice()/ManagermentSystemConstant.PRICE_TRANSITION));

        return foodMaterialManager;
    }

    @Override
    public void update(FoodMaterialManager foodMaterialManager) {
        String unitPriceString = foodMaterialManager.getUnitPriceString();

        foodMaterialManager.setUnitPrice((int) (Double.valueOf(unitPriceString)*ManagermentSystemConstant.PRICE_TRANSITION));

        foodMaterialManagerMapper.updateByPrimaryKeySelective(foodMaterialManager);
    }

    @Override
    public void delelteById(String id) {
        foodMaterialManagerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(FoodMaterialManager foodMaterialManager) {
        String unitPriceString = foodMaterialManager.getUnitPriceString();

        foodMaterialManager.setUnitPrice((int) (Double.valueOf(unitPriceString)*ManagermentSystemConstant.PRICE_TRANSITION));

        foodMaterialManagerMapper.insertSelective(foodMaterialManager);
    }

    @Override
    @Transactional
    public void updateRepertory(FoodMaterialManager foodMaterialManager) {

        FoodMaterialManager old = foodMaterialManagerMapper.selectByPrimaryKey(foodMaterialManager.getId());

        Integer change = foodMaterialManager.getNowRepertory()-old.getNowRepertory();

        if ((int)change !=0) {

            inventoryParticularsService.save(foodMaterialManager.getId(),old.getNowRepertory(), change);
        }

        foodMaterialManagerMapper.updateByPrimaryKeySelective(foodMaterialManager);

    }
}
